package game.assist.diablo3.activitys;

import game.assist.diablo3.R;
import game.assist.diablo3.db.DiabloDatabaseHelper;
import game.assist.diablo3.ui.TabPageIndicator;
import game.assist.diablo3.ui.VerticalTabPageIndicator;
import game.assist.diablo3.util.DialogUtil;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

public abstract class BaseAnimationActivity extends MenuActivity {

	// define animation

	protected int mWidth;
	public static TranslateAnimation sLeftInAnimation;
	public static TranslateAnimation sLeftOutAnimation;
	public static TranslateAnimation sRightOutAnimation;
	public static TranslateAnimation sRightInAnimation;

	protected int mHeight;
	protected static TranslateAnimation sUpInAnimation;
	protected static TranslateAnimation sUpOutAnimation;
	protected static TranslateAnimation sDownOutAnimation;
	protected static TranslateAnimation sDownInAnimation;

	protected ViewFlipper viewFlipper;
	protected LayoutInflater mInflater;

	protected View mFirstView;

	protected TabPageIndicator tabPageIndicator;
	protected LinearLayout leftTabIndicator;
	protected VerticalTabPageIndicator verticalTabIndicator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mWidth = getWindowManager().getDefaultDisplay().getWidth();
		mHeight = getWindowManager().getDefaultDisplay().getHeight();
		mInflater = LayoutInflater.from(getApplicationContext());

		initAnimation();

		setContentView(R.layout.main_indr);
		viewFlipper = (ViewFlipper) findViewById(R.id.flipper_content);
		tabPageIndicator = (TabPageIndicator) findViewById(R.id.category_main);
		leftTabIndicator = (LinearLayout) findViewById(R.id.verLayout);
		// verticalTabIndicator = (VerticalTabPageIndicator)
		// findViewById(R.id.category_child);

		// mFirstView = initTheFirstView();
		// viewFlipper.addView(mFirstView);
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	private BroadcastReceiver mBackReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			goBackView();
		}
	};

	protected abstract View initTheFirstView();

	private void initAnimation() {
		float f = -mWidth;
		sLeftOutAnimation = new TranslateAnimation(0F, f, 0F, 0F);
		float f1 = mWidth;
		sRightInAnimation = new TranslateAnimation(f1, 0F, 0F, 0F);
		float f2 = -mWidth;
		sLeftInAnimation = new TranslateAnimation(f2, 0F, 0F, 0F);
		float f3 = mWidth;
		sRightOutAnimation = new TranslateAnimation(0F, f3, 0F, 0F);

		float h = -mHeight;
		sUpOutAnimation = new TranslateAnimation(0F, 0F, 0F, h);
		float h1 = mHeight;
		sDownInAnimation = new TranslateAnimation(0F, 0F, h1, 0F);
		float h2 = -mHeight;
		sUpInAnimation = new TranslateAnimation(0F, 0F, h2, 0F);
		float h3 = mHeight;
		sDownOutAnimation = new TranslateAnimation(0F, 0F, 0F, h3);

		sLeftOutAnimation.setDuration(450L);
		sRightInAnimation.setDuration(450L);
		sLeftInAnimation.setDuration(450L);
		sRightOutAnimation.setDuration(450L);
		sUpInAnimation.setDuration(450L);
		sUpOutAnimation.setDuration(450L);
		sDownOutAnimation.setDuration(450L);
		sDownInAnimation.setDuration(450L);
	}

	private boolean exitFlag = false;

	@Override
	public void onBackPressed() {
		// goBackView();
		if (exitFlag) {
			finish();
		} else {
			DialogUtil.showShortToastInfo(getApplicationContext(), getString(R.string.s_double_exit));
			exitFlag = true;
		}

		new Thread(new Runnable() {

			@Override
			public void run() {
				Looper.prepare();

				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				exitFlag = false;

				Looper.loop();

			}
		}).start();
	}

	protected boolean finishApplication() {
		if (mFirstView == viewFlipper.getCurrentView()) {

			// TODO show a dialog tip
			finish();
			return true;
		}
		return false;
	}

	protected void goBackView() {
		if (!finishApplication()) {
			showPrevious();
		}
	}

	protected void showPrevious() {
		View currentView = viewFlipper.getCurrentView();
		viewFlipper.setOutAnimation(sRightOutAnimation);
		viewFlipper.setInAnimation(sLeftInAnimation);
		viewFlipper.showPrevious();
		viewFlipper.removeView(currentView);
	}

	protected void showPreviousUp() {
		View currentView = viewFlipper.getCurrentView();
		viewFlipper.setOutAnimation(sDownOutAnimation);
		viewFlipper.setInAnimation(sUpInAnimation);
		viewFlipper.showPrevious();
		viewFlipper.removeView(currentView);
	}

	protected void showNext(View view) {
		viewFlipper.setOutAnimation(sLeftOutAnimation);
		viewFlipper.setInAnimation(sRightInAnimation);
		viewFlipper.addView(view);
		viewFlipper.showNext();
	}

	protected void showNextQuick(View view) {
		viewFlipper.addView(view);
		viewFlipper.showNext();
	}

	protected void showNextDown(View view) {
		viewFlipper.setOutAnimation(sUpOutAnimation);
		viewFlipper.setInAnimation(sDownInAnimation);
		viewFlipper.addView(view);
		viewFlipper.showNext();
	}

	public void goXXPage() {
		// showNext(new XXView(this));
	}

}
